<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BackstageModel extends Model
{
	// 顶部导航栏
	// static public function getList($data = array()){
 //    	return self::menu();
 //    }
    // 更具菜單等級獲取最大pid
    static public function getMaxPid($table,$level=0){
        if($level>0){
            for ($i=0; $i <$level ; $i++){
                if($i==0){
                    $res = DB::table($table)->select('id')->where('level','=',$i)->orderBy('sort_order','desc')->orderBy('id','desc')->first();
                }else{
                    $res = DB::table($table)->select('id')->where('level','=',$i)->where('pid','=',$res->id)->orderBy('sort_order','desc')->orderBy('id','desc')->first();
                }
            }
        }else{
            $res = DB::table($table)->select('id')->where('level','=',$level)->orderBy('sort_order','desc')->orderBy('id','desc')->first();
        }
        return $res->id;
    }
    // 菜单
    static public function menu($pid=0,$list=array(),$lv=0){
		$str='';
		if($pid != 0){
            $p_id = self::getMaxPid('menu',$lv+1);
            if($p_id==$pid){
                for ($i=0; $i <$lv ; $i++){
                    $str.='<i class="tree">&nbsp;&nbsp;&nbsp;&nbsp;</i>';
                }
            }else{
                // echo $lv;
                for ($i=0; $i <$lv ; $i++){
                    $str.='<i class="tree">│&nbsp;&nbsp;</i>';
                }
            }
			$lv++;
		}
		$arr = DB::table('menu')->where(['pid'=>$pid])->orderBy('sort_order','desc')->orderBy('id','asc')->get();
		foreach ($arr as $k=> $val) {
			if($pid != 0){
				if( count($arr) == ($k+1) && $lv>1 ){
                    $val->menu_name=$str.'<i class="tree">└─</i><i class="fa '.$val->icon.'"></i><span class="m_name">'.$val->menu_name.'</span>';
				}else{
                    if($pid==$p_id && $lv==1){
                        $val->menu_name=$str.'<i class="tree">└─</i><i class="fa '.$val->icon.'"></i><span class="m_name">'.$val->menu_name.'</span>';
                    }else{
                        $val->menu_name=$str.'<i class="tree">├─</i><i class="fa '.$val->icon.'"></i><span class="m_name">'.$val->menu_name.'</span>';
                    }
				}
			}else{
                $val->menu_name='<i class="fa '.$val->icon.'"></i><span class="m_name">'.$val->menu_name.'</span>';
            }
            $sonCount = self::getCount('menu',['pid'=>$val->id]);//判断是否有子菜单
            $val->son_count = $sonCount;
            $list[]=$val;
            if($sonCount>0){
                $list=self::menu($val->id,$list,$lv);
            }
		}
		return $list;
	}

    // 文章分类
    static public function getCateList($pid=0,$list=array(),$lv=0){
        $str='';
        for ($i=0; $i <$lv ; $i++){
            $str.='&nbsp;&nbsp;&nbsp;&nbsp;';
        }
        $lv++;
        $arr = DB::table('article_category')->where(['pid'=>$pid])->orderBy('sort_order','desc')->orderBy('id','asc')->get();
        foreach ($arr as $k=> $val) {
            $val->type_name=$str.$val->type_name;
            $sonCount = self::getCount('article_category',['pid'=>$val->id]);//判断是否有子菜单
            $val->son_count = $sonCount;
            $list[]=$val;
            if($sonCount>0){
                $list=self::getCateList($val->id,$list,$lv);
            }
        }
        return $list;
    }
    // 获取菜单里面权限列表
    static public function permissionList($pid=0,$list=array()){
        $arr = DB::table('menu')->select('id','pid','level','menu_name','action','has_permissions')->where(['pid'=>$pid])->orderBy('sort_order','desc')->orderBy('id','asc')->get();
        foreach ($arr as $k=> $val){
            if($val->has_permissions!='') $val->has_permissions=unserialize($val->has_permissions);
            $list[]=$val;
            $sonCount = self::getCount('menu',['pid'=>$val->id]);
            if($sonCount>0){
                $list = self::permissionList($val->id,$list);
            }
        }
        return $list;
    }

    // 獲取列表個數
    static public function getCount($table,$map=array()){
        if(!empty($map)){
            return DB::table($table)->where($map)->count();
        }else{
            return DB::table($table)->count();
        }
    }

	// 获取菜单
    static public function getMenu($map = array()){
    	return DB::table('menu')->where($map)->orderBy('sort_order','desc')->orderBy('id','asc')->get();
    }
    // 根据ID获取上级
    static public function getMenuById($id,$guide = array()){
        $vl = DB::table('menu')->select('id','menu_name','pid','icon','has_permissions')->where('id','=',$id)->first();
        $vl->son = count($guide);
        if($vl->has_permissions!='') $vl->has_permissions=unserialize($vl->has_permissions);
        array_unshift($guide,$vl);
        if($vl->pid!=0){
            $guide = self::getMenuById($vl->pid,$guide);
        }
        return $guide;
    }

    // 获取地区列表
    static public function getAreaList($pid=0,$list=array(),$lv=0){
        // $str='';
        // if($pid != 0){
        //     for ($i=0; $i <$lv ; $i++) { 
        //         $str.='&nbsp;&nbsp;&nbsp;&nbsp;';
        //     }
        //     $lv++;
        // }
        // $arr = DB::table('area')->select('id','pid','name','sort_order','level')->where('pid','=',$pid)->orderBy('sort_order','desc')->orderBy('id','asc')->get();
        // foreach ($arr as $k=> $val){
        //     if($pid != 0){
        //         $val->name=$str.'→'.$val->name;
        //     }else{
        //         $val->name=$val->name;
        //     }
        //     $list[]=$val;
        //     $sonCount = self::getCount('area',['pid'=>$val->id]);
        //     if($sonCount>0){
        //         $list = self::getAreaList($val->id,$list,$lv);
        //     }
        // }
        // return $list;
        return DB::table('area')->select('id','pid','name','sort_order','level')->orderBy('id','asc')->get();
    }

    // 获取网站参数
    static public function getGlobal(){
    	return DB::table('global')->orderBy('sort_order','desc')->get();
    }

    // 获取分类
    static public function getCate($table,$map = array()){
    	if(!empty($map)){
    		return DB::table($table)->where($map)->orderBy('sort_order','desc')->get();
    	}else{
    		return DB::table($table)->orderBy('sort_order','desc')->get();
    	}
    	
    }

    // 获取管理员列表
    static public function getAdminList($field='a.*,g.role,g.authority',$map=array(),$count=15){
        return DB::table(DB::raw('zzb_admin a'))->leftJoin(DB::raw('zzb_admin_group g'), DB::raw('g.id'), '=', DB::raw('a.u_group_id'))->select(DB::raw($field))->where($map)->paginate($count);
    }
    
    // 获取一个管理员
    static public function getOneAdmin($map=array()){
    	return DB::table('admin')->where($map)->first();
    }

    // 获取用户组权限列表
    static public function getGroupList($field='*',$map=array(),$count=15){
        return DB::table('admin_group')->select($field)->where($map)->orderBy('id','asc')->paginate($count);
    }

    // 获取一个用户组
    static public function getOneGroup($map=array()){
        return DB::table('admin_group')->where($map)->first();
    }
    // 獲取资金变动记录
    static public function getMoneyLogList($field='',$map='',$count=15){
        // DB::enableQueryLog();
        return DB::table(DB::raw('zzb_user_moneylog l'))->leftJoin(DB::raw('zzb_user u'), DB::raw('u.id'), '=', DB::raw('l.uid'))->select(DB::raw($field))->orderBy(DB::raw('l.id'),'desc')->whereRaw($map)->paginate($count);
        // $queries = DB::getQueryLog();
        // dd($queries);
    }
    // 獲取管理员操作日志
    static public function getOperationLogList($field='',$map='',$count=15){
        return DB::table(DB::raw('zzb_operation_log l'))->leftJoin(DB::raw('zzb_admin a'), DB::raw('a.id'), '=', DB::raw('l.create_id'))->select(DB::raw($field))->orderBy(DB::raw('l.id'),'desc')->whereRaw($map)->paginate($count);
    }

}
